package com.tutu.service.impl;

import com.tutu.core.Constant;
import com.tutu.core.api.ArticleApiUrls;
import com.tutu.service.ArticleService;
import com.tutu.utils.HttpRequestUtil;
import com.tutu.utils.UnicodeUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Override
    public Map<String, Object> pageByParams(Integer type, String keyWord, Integer pageNum, Integer pageSize) {
        return HttpRequestUtil.getOnlyData(ArticleApiUrls.base_prefix + ArticleApiUrls.page, "type=" + type + "&keyWord=" + UnicodeUtil.unicodeEncode(keyWord) + "&pageNum=" + pageNum + "&pageSize=" + pageSize);
    }

    @Override
    public Map<String, Object> saveInfo(HttpServletRequest request, Integer id, Integer type, String avatar, String title, String content) {
        LogInfoServiceImpl.saveLogInfo(request, "编辑文章，ID：" + id + "，类型：" + type + "，标题：" + title);

        return HttpRequestUtil.getCompleteData(ArticleApiUrls.base_prefix + ArticleApiUrls.update, "id=" + id
                + "&type=" + (null == type ? Constant.IMAGE_TYPE_HOME : type) + "&avatar=" + UnicodeUtil.unicodeEncode(avatar)
                + "&title=" + UnicodeUtil.unicodeEncode(title) + "&content=" + UnicodeUtil.unicodeEncode(content));
    }

    @Override
    public Map<String, Object> deleteById(HttpServletRequest request, Integer id) {
        LogInfoServiceImpl.saveLogInfo(request, "删除文章，ID：" + id);

        return HttpRequestUtil.getCompleteData(ArticleApiUrls.base_prefix + ArticleApiUrls.delete, "id=" + id);
    }

    @Override
    public Map<String, Object> getInfoById(Integer id) {
        return HttpRequestUtil.getOnlyData(ArticleApiUrls.base_prefix + ArticleApiUrls.info, "id=" + id);
    }
}
